<?php
// 应用公共文件
use think\facade\Cache;
use app\logic\DataHelper;

function is_str_empty($str) {
    if ($str === '') {
        return true;
    }

    return false;
}

function get_all_params() {
    $params = array_merge($_GET, $_POST);
    return $params;
}

function check_params_empty($fields = []) {
    $params = get_all_params();

    foreach ($params as $key => $value) {
        if (in_array($key, $fields) && is_str_empty($value)) {
            return false;
        }
    }

    return true;
}

function time_now() {
    return Date('Y-m-d H:i:s', time());
}

function get_curr_admin() {
    $token = cookie("admin_token");
    if (empty($token)) return null;

    $admin = Cache::get($token);
    if (empty($admin)) return null;


    $admin = DataHelper::find_by_id('tb_admin', $admin['id']);

    //var_dump($user);

    //延长token有效期
    Cache::set($token, $admin, 3600);

    return $admin;
}

function str_left($str, $len) {
    if (mb_strlen($str) <= $len) return $str;

    $str = mb_substr($str, 0, $len);
    return $str . '...';
}